package com.linkstec.bookshop.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.linkstec.bookshop.pojo.Admin;
import com.linkstec.bookshop.pojo.User;
import com.linkstec.bookshop.service.AdminService;
import com.linkstec.bookshop.service.UserService;

@Controller
@RequestMapping("login")
public class LoginController {

	@Autowired
	private UserService userService;
	@Autowired
	private AdminService adminService;

	@RequestMapping(value = "toLogin")
	public String toLogin() {
		return "login";
	}

	@RequestMapping(value = "login")
	private String login(HttpServletRequest req, User user, String checkImg) {

		String sessionCode = (String) req.getSession().getAttribute("checkcode");
		if (!sessionCode.equalsIgnoreCase(checkImg)) {
			req.getSession().setAttribute("message", "验证码输入错误请重新登录");
			return "redirect:/login/toLogin";
		}
		User loginUser = new User();

		try {
			loginUser = userService.findUserByNameAndPwd(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (loginUser==null) {
			 req.getSession().setAttribute("message", "密码输入错误请重新登陆");
			 return "redirect:/login/toLogin";
		}
		
		req.getSession().setAttribute("loginUser", loginUser);
		req.getSession().removeAttribute("message");
		return "redirect:/user/index";

	}

	/**     
	 * @Description:TODO 管理员登陆
	 * @author: yanyuan
	 * @date:   2018年11月23日 下午2:22:04    
	 *  @param req
	 *  @param admin
	 *  @param checkImg
	 *  @return    
	 */ 
	@RequestMapping(value = "adminLogin")
	private String adminLogin(HttpServletRequest req, Admin admin, String checkImg) {

		String sessionCode = (String) req.getSession().getAttribute("checkcode");
		if (!sessionCode.equalsIgnoreCase(checkImg)) {
			req.getSession().setAttribute("message", "验证码输入错误请重新登录");
			return "redirect:/login/toAdmin";
		}
		Admin loginAdmin = new Admin();

		try {
			loginAdmin = adminService.findAdminByNameAndPwd(admin);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (loginAdmin==null) {
			 req.getSession().setAttribute("message", "密码输入错误请重新登陆");
			 return "redirect:/login/toAdmin";
		}
		
		req.getSession().setAttribute("loginAdmin", loginAdmin);
		req.getSession().removeAttribute("message");
		return "redirect:/login/toHome";

	}
	
	@RequestMapping(value = "toAdmin")
	public String toAdmin() {
		return "admin/index";
	}
	
	@RequestMapping(value = "toHome")
	public String toIndex() {
		return "admin/home";
	}

	@RequestMapping(value="loginOut")
	public String loginOut(HttpServletRequest req){
		req.getSession().removeAttribute("loginUser");
		
		return "redirect:/user/index";
	}
	
	@RequestMapping(value="loginOutAdmin")
	public String loginOutAdmin(HttpServletRequest req){
		req.getSession().removeAttribute("loginAdmin");
		
		return "redirect:/login/toAdmin";
	}
}
